FROM golang:1.24-alpine AS base

RUN apk add --no-cache \
  build-base \
  protobuf \
  protobuf-dev \
  git \
  wget \
  make \
  nodejs \
  yarn

# Newer versions of protoc-gen-go are stricter with the `go_package` argument, which is invalid in the K8s protobufs
# so we're currently locked to an older version as there's no way to suppress the error
# see https://github.com/golang/protobuf/issues/1316
RUN go install 'google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.0'
RUN go install 'google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2.0'
RUN go install 'github.com/google/wire/cmd/wire@v0.5.0'
RUN go install 'golang.org/x/tools/cmd/goimports@latest'
ARG GRPC_GATEWAY_VERSION="v1.16.0"
RUN go install "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@${GRPC_GATEWAY_VERSION}"
RUN go install "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@${GRPC_GATEWAY_VERSION}"

RUN wget https://github.com/grpc/grpc-web/releases/download/1.0.6/protoc-gen-grpc-web-1.0.6-linux-x86_64 \
    -O /usr/local/bin/protoc-gen-grpc-web && \
  chmod +x /usr/local/bin/protoc-gen-grpc-web
  
RUN yarn global add \
  '@manifoldco/swagger-to-ts@1.4.0'

# Stage for webview-proto task
FROM base AS webview-proto-build
WORKDIR /go/src/github.com/tilt-dev/tilt
COPY pkg/webview/log.proto pkg/webview/log.proto
COPY pkg/webview/view.proto pkg/webview/view.proto
COPY pkg/apis/core/v1alpha1/generated.proto pkg/apis/core/v1alpha1/generated.proto
COPY vendor/k8s.io/apimachinery/pkg/runtime/generated.proto vendor/k8s.io/apimachinery/pkg/runtime/generated.proto
COPY vendor/k8s.io/apimachinery/pkg/runtime/schema/generated.proto vendor/k8s.io/apimachinery/pkg/runtime/schema/generated.proto
COPY vendor/k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto vendor/k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto

RUN mkdir -p web/src && \
    protoc \
    -I. \
    -I./vendor \
    -I/usr/include \
    -I"$(go list -f '{{.Dir}}' -m 'github.com/grpc-ecosystem/grpc-gateway@latest')/third_party/googleapis" \
    --grpc-gateway_out=logtostderr=true:. \
    --swagger_out=logtostderr=true:. \
    --go_out=. --go_opt=paths=source_relative \
    --go-grpc_out=. --go-grpc_opt=paths=source_relative \
    pkg/webview/*.proto && \
    sed -i 's|v1alpha1 "pkg/apis/core/v1alpha1"|v1alpha1 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"|g' pkg/webview/*.pb.go && \
    sed -i 's|"$ref": "#/definitions/v1Time",|"type": "string", "format": "date-time",|g' pkg/webview/view.swagger.json && \
    sed -i 's|"$ref": "#/definitions/v1MicroTime",|"type": "string", "format": "date-time",|g' pkg/webview/view.swagger.json && \
    goimports -local github.com/tilt-dev -w pkg/webview/*.pb.go

# Output stage for webview-proto task
FROM scratch AS webview-proto-output
COPY --from=webview-proto-build /go/src/github.com/tilt-dev/tilt/pkg/webview/log.pb.go /go/src/github.com/tilt-dev/tilt/pkg/webview/log.pb.go
COPY --from=webview-proto-build /go/src/github.com/tilt-dev/tilt/pkg/webview/view.pb.go /go/src/github.com/tilt-dev/tilt/pkg/webview/view.pb.go
COPY --from=webview-proto-build /go/src/github.com/tilt-dev/tilt/pkg/webview/view_grpc.pb.go /go/src/github.com/tilt-dev/tilt/pkg/webview/view_grpc.pb.go
COPY --from=webview-proto-build /go/src/github.com/tilt-dev/tilt/pkg/webview/view.swagger.json /go/src/github.com/tilt-dev/tilt/pkg/webview/view.swagger.json

# Stage for proto-ts task
FROM base AS proto-ts-build
WORKDIR /go/src/github.com/tilt-dev/tilt
COPY --from=webview-proto-build /go/src/github.com/tilt-dev/tilt/pkg/webview/view.swagger.json pkg/webview/view.swagger.json

RUN <<EOF
    mkdir -p web/src && \
    swagger-to-ts pkg/webview/view.swagger.json --camelcase --wrapper "
    // Autogenerated: DO NOT EDIT
    declare namespace Proto
    " --output web/src/view.d.ts
EOF

# Output stage for proto-ts task
FROM scratch AS proto-ts-output
COPY --from=proto-ts-build /go/src/github.com/tilt-dev/tilt/web/src/view.d.ts /go/src/github.com/tilt-dev/tilt/web/src/view.d.ts
